const request = require('request');
// 引入数据库
const {
  mysqlFun
} = require('../mysql/mysql');

exports.getLife = function (app) {
  // 历史上的今天
  app.get('/lifeHistory', (req, res) => {
    let date = new Date();
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    request(`http://api.juheapi.com/japi/toh?v=1.0&month=${month}&day=${strDate}&key=3d68d536e1f96032b1f3855118e6f429`, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        body = JSON.parse(body);
        res.json({
          code: 'F0001',
          msg: '请求返回数据成功',
          data: body
        })
        return;
      }
      res.json({
        code: 'F0002',
        msg: '今日请求已达上限'
      })
    })
  })

  // 号码归属地查询
  app.post('/lifePhone', (req, res) => {
    request(`http://apis.juhe.cn/mobile/get?phone=${req.body.phone}&key=f806857056ea82b218450b19a7fdd98c`, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        res.json({
          code: 'F0003',
          msg: '号码归属地内容返回成功',
          data: JSON.parse(body)
        })
        return;
      }
      res.json({
        code: 'F0004',
        msg: '当日可用请求数量已达上限'
      })
    })
  })

  // 天气查询
  app.post('/lifeWeather', (req, res) => {
    let weather = encodeURI(req.body.weather.substr(0, req.body.weather.length - 1));
    request(`http://apis.juhe.cn/simpleWeather/query?city=${weather}&key=b75b40c82d38885779e9ae63d17a12dd`, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        res.json({
          code: 'F0005',
          msg: '天气查询成功',
          data: JSON.parse(body)
        })
        return;
      }
      res.json({
        code: 'F0006',
        msg: '暂无该城市天气信息'
      })
    })
  })

  // ip地址查询查询
  app.post('/lifeIp', (req, res) => {
    request(`http://apis.juhe.cn/ip/ipNew?ip=${req.body.ip}&key=6607105cf5b2f48cd5970093a820e48e`, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        res.json({
          code: 'F0007',
          msg: 'ip地址查询成功',
          data: JSON.parse(body)
        })
        return;
      }
      res.json({
        code: 'F0008',
        msg: '当日可用请求数量已达上限'
      })
    })
  })

  // 对话机器人
  app.post('/lifechat', (req, res) => {
    let word = encodeURI(req.body.text)
    request(`http://tool.mkblog.cn/robot/new.php?word=${word}`, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        res.json({
          code: 'F0009',
          msg: '返回对话成功',
          data: JSON.parse(body)
        })
        return;
      }
      res.json({
        code: 'F0010',
        msg: '当日可用请求数量已达上限'
      })
    })
  })

  // 获取qq用户名
  app.post('/getQqUser', (req, res) => {
    let qqUser = encodeURI(req.body.qqUser)
    request(`http://api.o5tc.cn/qqxt/api.php?qq=${qqUser}`, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        res.json({
          code: 'F0066',
          msg: '获取qq用户名成功',
          user: JSON.parse(body).name
        })
        return;
      }
    })
  })

  // 获取网易云热评
  app.post('/HotComments', (req, res) => {
    request(`https://api.molure.cn/wyyrp/api.php?format=json`, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        res.json({
          code: 'W0001',
          msg: '获取网易云热评成功',
          user: JSON.parse(body)
        })
        return;
      }
    })
  })

  // 获取图片
  app.post('/Picture', (req, res) => {
    function picture(cla, int) {
      let pictureInt = [];
      for (let index = 0; index < int; index++) {
        pictureInt.push(`https://api.molure.cn/sjbz/api.php?method=mobile&lx=${cla}&index=${index}`);
      }
      return pictureInt;
    }
    let bodyAll = [];
    switch (req.body.class) {
      case 'fengjing':
        bodyAll.push(picture('fengjing', req.body.int))
        break;
      case 'dongman':
        bodyAll.push(picture('dongman', req.body.int))
        break;
      case 'meizi':
        bodyAll.push(picture('meizi', req.body.int))
        break;
      case 'suiji':
        bodyAll.push(picture('suiji', req.body.int))
        break;
      default:
        break;
    }
    res.json({
      code: 'P0001',
      msg: '图片获取成功',
      picture: bodyAll[0]
    })
  })

  // 获取舔狗日记
  app.post('/getFlatterer', (req, res) => {
    request(`http://xiaoqiapi.52msr.cn/tgo/api.php?encode=json`, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        res.json({
          code: 'F0001',
          msg: '获取舔狗日记成功',
          text: body
        })
        return;
      }
    })
  })

  // 上传长网址转短网址
  app.post('/getShort', (req, res) => {
    // 生成一个十位数的id码
    let str = Math.random().toString(36).substr(2);
    let sql = `INSERT INTO store(urlid,url) value ('${str}','${req.body.url}')`;

    function obj(data) {
      if (data === undefined) {
        return;
      }
      if (data.length !== 0) {
        res.json({
          code: 'S0001',
          msg: '短网址生成',
          url: str
        })
      }
    }
    mysqlFun(sql, obj);
  })

  // 短视频
  app.get('/url/*', (req, res) => {
    // 去掉全面的url
    let url = req.path.replace('/url/', "");
    let sql = `select * from store`;

    function obj(data) {
      if (data === undefined) {
        return;
      }
      if (data.length !== 0) {
        for (let index = 0; index < data.length; index++) {
          let urllist = data[index];
          // url的后缀在服务器中存在就返回对应的url
          if (urllist.urlid === url) {
            res.redirect(301, urllist.url);
            return;
          }
        }
        res.json({
          code: '404',
          msg: '找不到你的网页'
        })
      }
    }
    mysqlFun(sql, obj);
  })

  // 获取所有的短网址
  app.post('/cloneUrlAll', (req, res) => {
    let sql = `select * from store`;

    function obj(data) {
      if (data === undefined) {
        return;
      }
      if (data.length !== 0) {
        res.json({
          code: 'I0001',
          msg: '短网址获取成功',
          All: data
        })
      }
    }
    mysqlFun(sql, obj);
  })

  // 删除指定的网址
  app.post('/deleteUrl', (req, res) => {
    let sql = `DELETE FROM store WHERE urlid='${req.body.urlid}'`;

    function obj(data) {
      if (data === undefined) {
        return;
      }
      if (data.length !== 0) {
        res.json({
          code: 'D0001',
          msg: '网址删除成功'
        })
      }
    }
    mysqlFun(sql, obj);
  })
}